//
//  StudyController.m
//  贝塞尔曲线
//
//  Created by 宜必鑫科技 on 2017/11/1.
//  Copyright © 2017年 宜必鑫科技. All rights reserved.
//

#import "StudyController.h"
/** 宏. */
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height


@interface StudyController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UIView *disPlayView;
@end

@implementation StudyController


/**
  http://www.jianshu.com/p/7c03f7e9ed57
  UIBezierPath ios 中用来绘制矢量图
  CAShapeLayer 用来负责显示
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.array = @[@"清除",@"折线",@"三角形",@"多边形",@"椭圆",@"实心矩形",@"空心矩形",@"圆角矩形",@"单脚矩形",@"弧线",@"圆弧和折线",@"二次贝塞尔曲线",@"三次贝塞尔曲线",@"曲面",@"动画",@"WelcomLabel",@"脸"];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 10;
    
    _collView = [[UICollectionView alloc]initWithFrame:CGRectMake(10,20,ScreenWidth - 20,ScreenHeight - ScreenWidth) collectionViewLayout:flow];
    [self.view addSubview:_collView];
    _collView.backgroundColor = [UIColor whiteColor];
    _collView.delegate = self;
    _collView.dataSource = self;
    _collView.bounces = YES;
    _collView.showsHorizontalScrollIndicator = NO;
    _collView.showsVerticalScrollIndicator = NO;
    [_collView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"RedioCollectionCell"];
    
    _disPlayView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - ScreenWidth, ScreenWidth, ScreenWidth)];
    _disPlayView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_disPlayView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
// cell大小的自适应
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *vc = self.array[indexPath.row];    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [vc boundingRectWithSize:CGSizeMake(1000, 25) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return CGSizeMake(rect.size.width, 25);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RedioCollectionCell" forIndexPath:indexPath];
    
    NSString *vc = self.array[indexPath.row];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [vc boundingRectWithSize:CGSizeMake(1000, 25) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, rect.size.width, 25)];
    label.backgroundColor = [UIColor lightGrayColor];
    label.text = vc;
    label.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:label];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        [self clear];
    }
    else if(indexPath.row == 1)
    {
        [self 折线];
    }
    else if(indexPath.row == 2)
    {
        [self 三角形];
    }
    else if(indexPath.row == 3)
    {
        [self 多边形];
    }
    else if(indexPath.row == 4)
    {
        [self 椭圆];
    }
    else if(indexPath.row == 5)
    {
        [self 实心矩形];
    }
    else if(indexPath.row == 6)
    {
        [self 空心矩形];
    }
    else if(indexPath.row == 7)
    {
        [self 圆角矩形];
    }
    else if(indexPath.row == 8)
    {
        [self 单脚矩形];
    }
    else if(indexPath.row == 9)
    {
        [self 弧线];
    }
    else if(indexPath.row == 10)
    {
        [self 圆弧和折线];
    }
    else if(indexPath.row == 11)
    {
        [self 二次贝塞尔曲线];
    }
    else if(indexPath.row == 12)
    {
        [self 三次贝塞尔曲线];
    }
    else if(indexPath.row == 13)
    {
        [self 曲面];
    }
    else if(indexPath.row == 14)
    {
        [self 动画];
    }
    else if (indexPath.row == 15)
    {
        [self createWelcomLabel];
    }
    else if (indexPath.row == 16)
    {
        [self 脸];
    }
}
- (void)clear
{
    for(UIView *view in [_disPlayView  subviews])
    {
        [view removeFromSuperview];
    }
    _disPlayView.layer.sublayers = nil;
    _disPlayView.backgroundColor = [UIColor lightGrayColor];
}
- (void)折线
{
   [self clear];
    
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:(CGPoint){20,20}];
    [linePath addLineToPoint:(CGPoint){160,160}];
    [linePath addLineToPoint:(CGPoint){200,20}];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 200, 200);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.lineWidth = 5;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.path = linePath.CGPath;
    layer.fillColor = nil; // 默认是黑色
    layer.lineCap = kCALineCapRound;
    
    [_disPlayView.layer addSublayer:layer];
}
- (void)三角形
{
    [self clear];
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, 20)];
    [path addLineToPoint:CGPointMake(160, 160)];
    [path addLineToPoint:CGPointMake(200, 20)];
    [path closePath]; // 封闭图像
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = _disPlayView.bounds;
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.lineWidth = 2;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.path = path.CGPath;
    layer.fillColor = [UIColor redColor].CGColor;
    layer.lineJoin = kCALineCapRound;   //  终点处理
    
    [_disPlayView.layer addSublayer:layer];
}
- (void)多边形
{
    [self clear];
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    
    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(100, 0.0)];
    
    // Draw the lines
    [aPath addLineToPoint:CGPointMake(200.0,40.0)];
    [aPath addLineToPoint:(CGPoint){160,140}];
    [aPath addLineToPoint:(CGPoint){40,140}];
    [aPath addLineToPoint:(CGPoint){0,40}];
    [aPath closePath];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.bounds = (CGRect){0,0,200,160};
    
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.lineWidth = 2.0;
    layer.lineCap = kCALineCapRound;  // 线条拐角
    layer.lineJoin = kCALineCapRound;   //  终点处理
    layer.strokeColor = [UIColor greenColor].CGColor;
    layer.path = aPath.CGPath;
    layer.fillColor = nil; // 默认为blackColor
    
    [_disPlayView.layer addSublayer:layer];
}
- (void)椭圆
{
    [self clear];
    
    // 线
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){0,0,260,200}];
    // 线的状态
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = (CGRect){0,0,260,260};
    pathLayer.position =  (CGPoint){ScreenWidth/2,ScreenWidth/2};
    pathLayer.lineWidth = 2;
    pathLayer.strokeColor = [UIColor redColor].CGColor;
    pathLayer.path = path.CGPath;
    pathLayer.fillColor = nil;
    [_disPlayView.layer addSublayer:pathLayer];
}
- (void)实心矩形
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 100, 100);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.backgroundColor = [UIColor orangeColor].CGColor;
    [_disPlayView.layer addSublayer:layer];
}
- (void)空心矩形
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 100, 100);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.lineWidth = 5;
    layer.strokeColor = [UIColor blackColor].CGColor;
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    [_disPlayView.layer addSublayer:layer];
}
- (void)圆角矩形
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 200, 200) cornerRadius:30];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 200, 200);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.fillColor = [UIColor orangeColor].CGColor;
    layer.path = path.CGPath;
    [_disPlayView.layer addSublayer:layer];
}
- (void)单脚矩形
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 200, 200) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(100, 0)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 200, 200);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.fillColor = [UIColor orangeColor].CGColor;
    layer.path = path.CGPath;
    [_disPlayView.layer addSublayer:layer];
}
- (void)弧线
{
    //    M_PI = 180;  M_PI_2 = 90; M_1_PI = 1/pai   M_2_PI = 2/pai
    // 3点钟方向为0度  最后一个为是否为顺时针
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:M_1_PI clockwise:NO];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 200, 200);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.path = path.CGPath;
    layer.lineWidth = 2;
    [_disPlayView.layer addSublayer:layer];
}

- (void)圆弧和折线
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:(CGPoint){100,100}];
    [path addArcWithCenter:(CGPoint){100,100} radius:100 startAngle:0 endAngle:M_PI clockwise:YES];
    [_disPlayView.layer addSublayer:[self getLayerWithPath:path]];
}
// 第一个参数为结束点
- (void)二次贝塞尔曲线
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 100)];
    [path addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(30, 30)];
    [_disPlayView.layer addSublayer:[self getLayerWithPath:path]];
}
- (void)三次贝塞尔曲线
{
    CGPoint startPoint = CGPointMake(0, 100);
    CGPoint endPoint = CGPointMake(200, 100);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:startPoint];
    [path addCurveToPoint:endPoint controlPoint1:CGPointMake(50, 75) controlPoint2:CGPointMake(150, 125)];
    [_disPlayView.layer addSublayer:[self getLayerWithPath:path]];
}
- (void)曲面
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 100)];
    [path addLineToPoint:CGPointMake(0, 200)];
    [path addLineToPoint:CGPointMake(200, 200)];
    [path addLineToPoint:CGPointMake(200, 100)];
    [path addQuadCurveToPoint:CGPointMake(0, 100) controlPoint:CGPointMake(30, 30)];
    [_disPlayView.layer addSublayer:[self getLayerWithPath:path]];
}

- (void)动画
{
    //画一个path
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, 100)];
    [path addLineToPoint:CGPointMake(ScreenWidth - 20, 100)];
    [path addLineToPoint:CGPointMake(ScreenWidth - 20, 200)];
    [path addLineToPoint:CGPointMake(20, 200)];
    [path addLineToPoint:CGPointMake(20, 300)];
    [path addLineToPoint:CGPointMake(ScreenWidth - 20, 300)];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.backgroundColor = [UIColor lightGrayColor].CGColor;
    layer.path = path.CGPath;
    layer.lineWidth = 2;
    [_disPlayView.layer addSublayer:layer];
    
    CABasicAnimation *checkAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    checkAnimation.duration =1.3f;
    checkAnimation.fromValue = [NSNumber numberWithFloat:0];
    checkAnimation.toValue = [NSNumber numberWithFloat:1.0];
    //    checkAnimation.autoreverses = YES;      //动画按原路返回
    //    checkAnimation.delegate = self;
    //    checkAnimation.repeatCount = MAXFLOAT;    //设置动画次数
    //    checkAnimation.repeatDuration = 8;        //设置动画持续时间
    checkAnimation.fillMode =kCAFillModeForwards;
    //    checkAnimation.beginTime = CACurrentMediaTime()+2;      //延迟2秒  CACurrentMediaTime() 为当前动画时间
    [layer addAnimation:checkAnimation forKey:nil];
   
    UIImageView * roundView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 96, 8, 8)];
    roundView.image = [UIImage imageNamed:@"bubble.png"];
    [_disPlayView addSubview:roundView];

    CAKeyframeAnimation* keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyFrameAni.repeatCount = NSIntegerMax;
    keyFrameAni.path = path.CGPath;
    keyFrameAni.duration = 15;
    keyFrameAni.beginTime = CACurrentMediaTime() + 2;
    [roundView.layer addAnimation:keyFrameAni forKey:@"keyFrameAnimation"];
}
- (void)createWelcomLabel
{
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{
        
        _disPlayView.backgroundColor = [UIColor colorWithRed:82/255.0f green:222/255.0f blue:178/255.0f alpha:1];
    } completion:^(BOOL finished) {
        UILabel *label = [[UILabel alloc] initWithFrame:self.view.frame];
        label.center = (CGPoint){ScreenWidth/2,ScreenWidth/2};
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:50.0];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"Welcome";
        label.transform = CGAffineTransformScale(label.transform, 0.25, 0.25);
        [_disPlayView addSubview:label];
        
        [UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            label.transform = CGAffineTransformScale(label.transform, 4.0, 4.0);
            
        } completion:^(BOOL finished) {
        }];
    }];
}


- (void)脸
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(80 - 80 * sqrt(2.0) / 2.0f,  80 + 80 * sqrt(2.0) / 2.0f)];
    [path addQuadCurveToPoint:CGPointMake(60, 80 - 20) controlPoint:CGPointMake(0, 80)];
    [path addLineToPoint:CGPointMake(100, 80 - 20)];
    [path addQuadCurveToPoint:CGPointMake(80 + 80 * sqrt(2.0) / 2.0f, 80 + 80 * sqrt(2.0) / 2.0f) controlPoint:CGPointMake(160, 80)];
    [path addArcWithCenter:(CGPoint){80,80} radius:80 startAngle:M_PI/4 endAngle:M_PI * 3/4 clockwise:YES];
    [_disPlayView.layer addSublayer:[self getLayerWithPath:path]];
}


- (CAShapeLayer *)getLayerWithPath:(UIBezierPath *)path
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 200, 200);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    layer.path = path.CGPath;
    layer.lineWidth = 2;
    
    CABasicAnimation *checkAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    checkAnimation.duration =1.3f;
    checkAnimation.fromValue = [NSNumber numberWithFloat:0];
    checkAnimation.toValue = [NSNumber numberWithFloat:1.0];
    checkAnimation.fillMode =kCAFillModeForwards;
    [layer addAnimation:checkAnimation forKey:nil];
    
    return layer;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(BOOL)shouldAutorotate
{
    return NO;
}

@end
